<?php
namespace Home\Controller;
use Think\Controller;
/**
*影厅管理
*@author Qiu Pengfei
*@since 1.0
*/
class RoomController extends BaseController
{
    //影厅主页
    public function index()
    {
        $model=M('room');
        $uid=session('Home.id');
        $count = $model->where('user_id=' . $uid)->count();
        $Page= new \Think\Page($count,2);
        $show = $Page->show();
        $rooms = $model->where('user_id=' . $uid)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('rooms',$rooms);
        $this->assign('page',$show);
        $this->assign('username',session('Home.username'));
        $this->display();
    }
    //修改影厅
    public function update(){
        if($_GET['id']&&$_GET['name']){
            $data['id']=I('GET.id');
            $data['name']=I('GET.name');
            $this->assign('data',$data);
            $this->display();
        }else{
            $this->redirect('Room/index');
        }
    }
    //处理影厅修改
    public function docreate(){
        $data['name']=I('POST.name');
        $data['id']=I('POST.id');
        $model=M('room');
        $resu=$model->where($data)->find();
        if($resu){
            echo 0;
            exit;
        }
        $data['updated_at']=time();
        $res=$model->where('id='.$_POST['id'])->save($data);
        echo $res;
    }
    //添加影厅
    public function addroom(){
        //把传过来的解析以下
        parse_str($_POST['name'],$ss);
        $model=M('room');
        $data['name']=$ss['name'];
        $data['user_id']=session('Home.id');
        $room=$model->where($data)->find();
        if($room){
            $this->error('影厅重复了！');
        }
        $data['created_at']=time();
        $room_id= $model->add($data);

        //座位管理
        $mo=M('room_seat');
        $dat['room_id'] =$room_id;
        $dat['row'] =$ss['row'];
        $dat['col'] =$ss['col'];
        $str ='';
        foreach($_POST['row-col'] as $key => $value){
            $str .=$value.',';
        }
        $dat['loss'] = trim($str,',');
        $seat= $mo->add($dat);
        if($seat){
            echo 1;
        }else{
            echo 0;
        }
    }
}